package com.fuss.activitytest.utils

import android.app.Activity

/**
 * <pre>
 *     author : Mr.Fu
 *     e-mail : 18622268981@163.com
 *     time   : 2021/07/26
 *     desc   : ActivityCollector 单例类, 作为 Activity 的集合
 *              单例类,只是在语法上模仿了静态方法的调用方式,
 *     version: 1.0
 *              2.0     给方法加上 { @JvmStatic} 注解,kotlin 编译器就会将这些方法编译成真正的静态方法
 * </pre>
 */
object ActivityCollector {

    private val activities = ArrayList<Activity>()

    /**
     * 向集合中添加 activity
     */
    @JvmStatic
    fun addActivity(activity: Activity) {

        activities.add(activity)
    }

    /**
     * 从集合中移除 activity
     */
    @JvmStatic
    fun removeActivity(activity: Activity) {

        activities.remove(activity)
    }

    /**
     * 将集合中存储的所有 activity 销毁
     */
    @JvmStatic
    fun finishAll() {

        for (activity in activities) {
            if (!activity.isFinishing) {
                activity.finish()
            }
        }
        activities.clear()
    }
}